package com.team06.dao;

import com.team06.pojo.BllsBelt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BllsBeltDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(BllsBeltDao.class.getName());
    private final JdbcTemplate jdbcTemplate;

    public BllsBeltDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void insertBelts(List<BllsBelt> belts) {
        if (belts == null || belts.isEmpty()) {
            return;
        }

        String sql = "INSERT INTO DFME_BLLS_BELT (" +
                " BTNO, BELT_ID, CODE, BTAT, ESTR, EEND, RSTR, REND, BTSC) " +
                "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try {
            for (BllsBelt belt : belts) {
                jdbcTemplate.update(sql,
                        belt.getBtno(),
                        belt.getBeltId(),
                        belt.getCode(),
                        belt.getBtat(),
                        belt.getEstr(),
                        belt.getEend(),
                        belt.getRstr(),
                        belt.getRend(),
                        belt.getBtsc()
                );
            }
            LOGGER.info("行李转盘数据插入成功（通过 Spring JDBC）");
        } catch (Exception e) {
            LOGGER.error("Failed to insert belts into database", e);
            throw new RuntimeException("Failed to insert belts into database", e);
        }
    }
}